<!--
 * @Author: 李金深 2896583081@qq.com
 * @Date: 2023-06-27 09:09:36
 * @LastEditors: 李金深 2896583081@qq.com
 * @LastEditTime: 2023-06-28 15:26:41
 * @FilePath: /com.huizhouyiren.web.d0101/src/components/audioControl/audioControl.vue
 * @Description: 音频播放 | 音频通知 相关
-->
<script setup>
import { ref, onMounted } from "vue"
import mitt from "@/mitt/index"
import tipMp3 from "@/assets/mp3/tip.mp3"

// 音频播放相关
let audioEle = ref(null)
let audioSrc = ref("")
let messageId = ""

// 音频的播放|暂停
mitt.on("audioControl", ({ src, id }) => {
    if (id != messageId) {
        mitt.emit("audioEnd", messageId)
        messageId = id
        audioSrc.value = src
        audioEle.value.src = src
        audioEle.value.play()
        return
    }
    if (audioEle.value.paused) {
        audioEle.value.play()
    } else {
        audioEle.value.pause()
    }

})
onMounted(() => {
    // 播放结束
    audioEle.value.addEventListener("ended", () => {
        mitt.emit("audioEnd", messageId)
    })
    // 播放错误
    audioEle.value.addEventListener("error", () => {
        mitt.emit("audioEnd", messageId)
        messageId = ''
        mitt.emit('messageTip', {
            type: 'error',
            msg: 'msg.playError',
            useTranslate: true
        })
    })
})

// 音频提示
let tipAudio = ref(null)
mitt.on("audioTip", () => {
    tipAudio.value.play()
})

</script>
<template>
    <audio ref="audioEle"></audio>
    <audio :src="tipMp3" ref="tipAudio"></audio>
</template>
<style></style>